<template>
  <view class="profile-container">
    <!-- 状态栏占位 -->
    <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>

    <!-- 用户信息区域 -->
    <view class="user-info-section">
      <view class="user-info-card">
        <view class="user-avatar">
          <view class="avatar-icon">
            <u-avatar :src="userInfo.avatar_url"></u-avatar>
          </view>
        </view>
        <view class="user-details">
          <view class="user-name">{{ userInfo.nick_name }}</view>
          <view class="user-id">ID：{{ userInfo.user_id }}</view>
        </view>
      </view>
    </view>

    <div v-if="userInfo.identity != 1" style="margin-left: 30rpx; margin-right: 30rpx;">
      <div @click="gotoInvitationCode"
        style="padding: 30rpx; background: white; border-radius: 16rpx; display: flex; justify-content: space-between;">
        <span>邀请码</span>
        <span>></span>
      </div>

      <div
        style="padding: 30rpx; background: white; border-radius: 16rpx; margin-top: 12rpx; display: flex; justify-content: space-between;">
        <span>优惠券</span>
        <span>></span>
      </div>
    </div>

    <!-- 功能菜单区域 -->
    <view class="menu-section" v-if="userInfo.identity === 1">
      <!-- 第一行功能 - 独立卡片 -->
      <view class="individual-cards-row">
        <view class="individual-card" @click="goToMyDevice">
          <view class="menu-icon">
            <image src="/static/new/wdsb.png" class="icon_image_icon" />
          </view>
          <text class="menu-text">我的设备</text>
        </view>
        <view class="individual-card" @click="goToMyInfo">
          <view class="menu-icon">
            <image src="/static/new/wdxx.png" class="icon_image_icon" />
          </view>
          <text class="menu-text">我的信息</text>
        </view>
        <view class="individual-card" @click="goToMyHealth">
          <view class="menu-icon">
            <image src="/static/new/wdjk.png" class="icon_image_icon" />
          </view>
          <text class="menu-text">我的健康</text>
        </view>
      </view>

      <!-- 基础服务卡片 -->
      <view class="service-card">
        <view class="service-title">基础服务</view>
        <view class="service-menu-row">
          <view class="menu-item" @click="goToMyPoints">
            <view class="menu-icon">
              <image src="/static/new/wdjf.png" class="icon_image_icon" />
            </view>
            <text class="menu-text">我的积分</text>
          </view>
          <view class="menu-item" @click="goToService">
            <view class="menu-icon">
              <image src="/static/new/sckf.png" class="icon_image_icon" />
            </view>
            <text class="menu-text">客服</text>
          </view>
          <view class="menu-item" @click="goToSettings">
            <view class="menu-icon">
              <image src="/static/new/sz.png" class="icon_image_icon" />
            </view>
            <text class="menu-text">设置</text>
          </view>
          <view class="menu-item" @click="goToRecommend">
            <view class="menu-icon">
              <image src="/static/new/tjhl.png" class="icon_image_icon" />
            </view>
            <text class="menu-text">推荐有礼</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="bottom-tabbar">
      <view class="tab-item" @click="switchTab('health')">
        <image src="/static/tabbar/home.png" class="tab-icon home-icon" />
        <text class="tab-text">首页</text>
      </view>
      <view class="tab-item" @click="switchTab('mall')" v-if="userInfo.identity === 1">
        <image src="/static/tabbar/shop.png" class="tab-icon home-icon" />
        <text class="tab-text">商城</text>
      </view>
      <view class="tab-item" @click="switchTab('consultation')" v-if="userInfo.identity === 1">
        <image src="/static/tabbar/chat.png" class="tab-icon home-icon" />
        <text class="tab-text">健康伙伴</text>
      </view>
      <view class="tab-item active">
        <image src="/static/tabbar/me_active.png" class="tab-icon home-icon" style="width: 54rpx; height: 68rpx;" />
        <text class="tab-text">我的</text>
      </view>
    </view>
  </view>
</template>

<script>
import { info } from '@/api/user';
export default {
  data() {
    return {
      statusBarHeight: 0,
      userInfo: {},
    };
  },

  onShow() {
    // this.getSystemInfo();
    this.getUserInfoFn();
  },

  methods: {
    // 跳转到邀请码
    gotoInvitationCode() {
      uni.navigateTo({
        url: "/pages/profile/invitationCode",
      });
    },
    async getUserInfoFn() {
      try {
        const _res = await info();
        this.userInfo = _res?.data?.userInfo;
        console.log('_res', _res);
      } catch (err) {
        console.log('报错了哈', err);
      }
    },
    getSystemInfo() {
      const systemInfo = uni.getSystemInfoSync();
      this.statusBarHeight = systemInfo.statusBarHeight || 0;
    },

    switchTab(tab) {
      switch (tab) {
        case 'health':
          uni.redirectTo({
            url: '/pages/health/index', success: function () {
            },
            fail: function (err) {
              console.log('跳转失败', err);


            },
          });
          break;
        case 'mall':
          uni.redirectTo({
            url: '/pages/mall/index', success: function () {
            },
            fail: function (err) {
              console.log('跳转失败', err);


            },
          });
          break;
        case 'consultation':
          uni.redirectTo({
            url: '/pages/consultation/index', success: function () {
            },
            fail: function (err) {
              console.log('跳转失败', err);


            },
          });
          break;
      }
    },

    // 跳转到我的设备
    goToMyDevice() {
      uni.navigateTo({
        url: '/pages/myDevice/index',
      });
    },

    // 跳转到我的信息
    goToMyInfo() {
      uni.navigateTo({
        url: '/pages/myInfo/index',
      });
    },

    // 跳转到我的健康
    goToMyHealth() {
      uni.navigateTo({
        url: '/pages/myHealth/index',
      });
    },

    // 跳转到我的积分
    goToMyPoints() {
      uni.navigateTo({
        url: '/pages/me/myPoints/index',
      });
    },

    // 跳转到客服
    goToService() {
      uni.showToast({
        title: '客服功能',
        icon: 'none',
      });
    },

    // 跳转到设置
    goToSettings() {
      uni.navigateTo({
        url: '/pages/settings/index',
      });
    },

    // 跳转到推荐有礼
    goToRecommend() {
      uni.navigateTo({
        url: '/pages/dealer/poster',
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.profile-container {
  min-height: 100vh;
  background: #f5f5f5;
  position: relative;
}

.status-bar {
  background: #ffffff;
}

.profile-navbar {
  height: 88rpx;
  background: #ffffff;
  position: relative;
  border-bottom: 1px solid #e5e5e5;

  .navbar-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    padding: 0 32rpx;

    .nav-left {
      flex: 1;
    }

    .nav-title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333333;
      text-align: center;
    }

    .nav-right {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-end;

      .nav-action-btn {
        width: 60rpx;
        height: 60rpx;
        background: rgba(0, 0, 0, 0.05);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 16rpx;

        .action-dots,
        .action-location {
          font-size: 24rpx;
          color: #666666;
        }
      }
    }
  }
}

.user-info-section {
  padding: 32rpx;

  .user-info-card {
    background: #ffffff;
    border-radius: 24rpx;
    padding: 40rpx;
    display: flex;
    align-items: center;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

    .user-avatar {
      width: 120rpx;
      height: 120rpx;
      background: #e3f2fd;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 32rpx;

      .avatar-icon {
        font-size: 48rpx;
        color: #2196f3;
      }
    }

    .user-details {
      .user-name {
        font-size: 40rpx;
        font-weight: bold;
        color: #333333;
        margin-bottom: 8rpx;
      }

      .user-id {
        font-size: 28rpx;
        color: #999999;
      }
    }
  }
}

.menu-section {
  padding: 0 32rpx;

  .individual-cards-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 24rpx;
    gap: 16rpx;

    .individual-card {
      flex: 1;
      background: #ffffff;
      border-radius: 24rpx;
      padding: 32rpx 16rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);



      .menu-icon {
        // "w-66rpx h-72rpx"
        width: 66rpx;
        height: 72rpx;
        border-radius: 20rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 16rpx;
        font-size: 32rpx;

        &>image {
          width: 100%;
          height: 100%;
        }

        &.device-icon {
          background: #e3f2fd;
          color: #2196f3;
        }

        &.info-icon {
          background: #e8f5e8;
          color: #4caf50;
        }

        &.health-icon {
          background: #fff3e0;
          color: #ff9800;
        }
      }

      .menu-text {
        font-size: 36rpx;
        color: #333333;
        text-align: center;
      }
    }
  }

  .service-card {
    background: #ffffff;
    border-radius: 24rpx;
    padding: 32rpx;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);

    .service-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333333;
      margin-bottom: 32rpx;
    }

    .service-menu-row {
      display: flex;
      justify-content: space-around;

      .menu-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        flex: 1;

        .menu-icon {
          width: 80rpx;
          height: 80rpx;
          border-radius: 20rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-bottom: 16rpx;
          font-size: 32rpx;

          &.care-icon {
            background: #e3f2fd;
            color: #2196f3;
          }

          &.service-icon {
            background: #fff8e1;
            color: #ffc107;
          }

          &.setting-icon {
            background: #e8f5e8;
            color: #4caf50;
          }

          &.share-icon {
            background: #e8f5e8;
            color: #4caf50;
          }
        }

        .menu-text {
          font-size: 36rpx;
          color: #333333;
          text-align: center;
        }
      }
    }
  }
}

.bottom-tabbar {
  padding-top: 20rpx;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  // height: 50px;
  background: #ffffff;
  display: flex;
  align-items: center;
  border-top: 1px solid #e5e5e5;
  padding-bottom: env(safe-area-inset-bottom);

  .tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .tab-icon {
      width: 72rpx;
      height: 72rpx;
      margin-bottom: 8rpx;
      border-radius: 8rpx;

      &.home-icon {
        // background: #2196f3;
      }

      &.mall-icon {
        background: #cccccc;
      }

      &.consultation-icon {
        background: #cccccc;
      }

      &.profile-icon {
        background: #cccccc;
      }
    }

    .tab-text {
      font-size: 36rpx;
      color: rgb(153, 153, 153);
    }

    &.active .tab-text {
      color: #006BFF;
    }
  }
}

.icon_image_icon {
  width: 66rpx;
  height: 72rpx;
}
</style>
